<template>
  <div class="article-container">
    <el-card class="filter-card">
      <div slot="header">
        <!-- 面包屑路径导航 -->
       <my-breadcrumb>内容管理</my-breadcrumb>
        <!-- /面包屑路径导航 -->
      </div>
      <!-- 数据筛选表单 -->
      <el-form ref="form" :model="form" label-width="40px" size="mini">
        <my-status v-model="form.status"></my-status>
       <MyChannel v-model="form.channelId"></MyChannel>
        <el-form-item label="日期">
          <el-date-picker
            v-model="form.dateRange"
            type="daterange"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="['12:00:00']">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="hSearch" :loading=isloading>查询</el-button>
        </el-form-item>
      </el-form>
      <!-- /数据筛选表单 -->
    </el-card>

    <el-card class="box-card">
      <div slot="header" class="clearfix">
        根据筛选条件共查询到{{total_count}} 条结果：
      </div>
      <!-- 数据列表 -->
      <el-table
        :data="tableData"
        stripe
        style="width: 100%"
        class="list-table"
        size="mini"
        v-loading="isloading"
      >
        <el-table-column
          label="封面"
      >
          <template slot-scope="scope">
            <!-- scope.row.cover.images[0] 是当前的图片地址 -->
           <el-image :src="scope.row.cover.images[0]" style="width: 100px; height: 100px">
              <div slot="placeholder" class="image-slot">
                加载中<span class="dot">...</span>
              </div>
            </el-image>
      </template>
        </el-table-column>
        <el-table-column
          prop="title"
          label="标题"
          width="400">
        </el-table-column>
        <el-table-column
          label="状态">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status==0">草稿</el-tag>
            <el-tag v-else-if="scope.row.status==1" type="success">待审核</el-tag>
            <el-tag v-else-if="scope.row.status==2" type="info">审核通过</el-tag>
            <el-tag v-else-if="scope.row.status==3" type="warning">审核失败</el-tag>
            <el-tag v-else-if="scope.row.status==4" type="danger">已删除</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="pubdate"
          label="发布时间">
        </el-table-column>
        <el-table-column
          prop="address"
          label="操作"
          width="160px"
          >
             <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              @click="hEdit(scope.$index, scope.row)">
            </el-button>

            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              @click="hDelete(scope.$index, scope.row)">
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- /数据列表 -->

      <!-- 列表分页 -->
      <el-pagination
        layout="prev, pager, next"
        background
        :total="total_count"
        @current-change="hPageChange"
        :disabled="isloading"
        >
      </el-pagination>
      <!-- /列表分页 -->
    </el-card>
  </div>
</template>

<script>
// 导入插件
import MyChannel from '@/components/channel.vue'
import MyStatus from '@/components/status.vue'
export default {
  name: 'ArticleIndex',
  components: {
    MyChannel, // 注册
    MyStatus
  },
  props: {},
  data () {
    return {
      form: {
        channelId: null, // 当前选中的
        status: null, // 查询文章的状态，不传这个参数，就是全部
        dateRange: null // 表示一个时间区间
      },
      tableData: [],
      total_count: 0,
      page: 1,
      isloading: false
    }
  },
  created () {
    this.loadArticles()
  },
  methods: {
    // 查询功能
    hSearch () {
      this.loadArticles()
    },
    // 编辑功能
    hEdit (index, row) {
      this.$router.push('/edit' + row.id.toString())
      console.log(index, row)
    },
    // 删除功能
    hDelete (index, row) {
      this.$confirm('你确定要删除吗？').then(async () => {
        const idStr = row.id.toString() // 定义个变量接受要删除的id
        try {
          // 发送请求
          const res = await this.$http({
            url: '/mp/v1_0/articles/' + idStr,
            method: 'DELETE'
          })
          console.log(res)
          this.$message.success('删除成功')
          this.loadArticles() // 重新渲染页面
        } catch (err) {
          console.log(err)
          this.$message.error('删除失败，只能删除草稿！')
        }
      })
    },
    // 分页功能
    hPageChange (page) {
      this.page = page
      this.loadArticles()
    },
    // 发请求获取数据信息
    async loadArticles () {
      const { dateRange, status, channelId } = this.form
      const queryObj = {
        page: this.page, // 页数
        status // 状态
      }
      // 判断用户是否选择了时间
      if (dateRange) {
        queryObj.begin_pubdate = dateRange[0] // 开始时间
        queryObj.end_pubdate = dateRange[1] // 末尾时间
      }
      if (channelId) {
        queryObj.channel_id = channelId
      } else {
        queryObj.channel_id = null
      }
      // 发送请求
      this.isloading = true
      try {
        const res = await this.$http({
          method: 'get',
          url: '/mp/v1_0/articles',
          params: queryObj
        })
        // 将后端的数据赋给自定义的变量
        this.tableData = res.data.data.results
        this.total_count = res.data.data.total_count // 数据的总数
        this.isloading = false
      } catch (err) {
        console.log(err)
        this.isloading = false
        this.$message.error('获取失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
.filter-card {
  margin-bottom: 30px;
}

.list-table {
  margin-bottom: 20px;
}
</style>
